﻿@using CodeGenerator.Core
@using System.Text
@{
    BuildHCodeCreate model = Model;
}
using HPK.Application.Entity;
using HPK.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HPK.Application.Busines
{
    @{
        string entity = model.Name + "Entity";
    }
    /// <summary>
    /// @(model.TableInfo.Comments)
    /// </summary>
    public class @(model.Name)Bll : BaseBll<@entity>
    {
    #region 查询数据
    /// <summary>
    /// @(model.TableInfo.Comments)
    /// </summary>
    /// <param name="keyValue">主键值</param>
    /// <returns></returns>
    public @entity GetEntity(string keyValue)
    {
    return db.GetEntity(keyValue);
    }

    /// <summary>
    /// 获取定时任务列表
    /// </summary>
    /// <param name="pagination">分页条件</param>
    /// <param name="keywords">查询条件</param>
    /// <returns></returns>
    public List<@entity>  GetPageList(Pagination pagination, string keywords)
    {
        return db.Exec(dbContext =>
        {
            int records = 0;

            var list = dbContext.Queryable<@entity>()
               // .WhereIF(!CommonHelper.IsEmpty(keywords), t => t.TaskTitle.Contains(keywords))
                .OrderByIF(!string.IsNullOrEmpty(pagination.sortField), pagination.sortField)
                .ToPageList(pagination.page, pagination.rows, ref records);
                pagination.records = records;

            return list;
         });
     }
     #endregion

     #region 提交数据
     /// <summary>
     /// 编辑数据
     /// </summary>
     /// <param name="entity"></param>
     /// <param name="KeyValue">主键</param>
     /// <returns></returns>
     public int Edit(@entity entity, string keyValue)
     {
        if (!CommonHelper.IsEmpty(keyValue))
        {
            entity.Modify(keyValue.ToString());

            return db.Update(entity);
        }
        else
        {
            entity.Create();
            return db.Insert(entity);
        }
     }

     /// <summary>
     /// 删除
     /// </summary>
     /// <param name="keyValue"></param>
     /// <returns></returns>
     public int Delete(string keyValue)
     {
        return db.Delete(t => t.TaskId == keyValue);
     }
     #endregion
   }
}
